
export default class CNZZ {
  constructor(cnzzId) {
    this.cnzzId = cnzzId;
  }
  push(content) {
    const _umuuid = (+new Date).toString(16) + parseInt(Math.random() * 1000).toString(16) + "-" + Math.random().toString(16).replace(".", "") + "-" + this._code() + "-" + (window.screen.height * window.screen.width).toString(16) + "-" + (+new Date).toString(16) + parseInt(Math.random() * 1000).toString(16);
    const _ntime = parseInt(new Date / 1000);
    const _cnzz_eid = parseInt(2147483648 * Math.random()) + "-" + _ntime + "-";
    const _showp = window.screen.width + "x" + window.screen.height;
    const _content = content.join('|');
    new Image().src = "//ei.cnzz.com/stat.htm?id=" + this.cnzzId + "&r=&lg=zh-cn&ntime=" + _ntime + "&cnzz_eid=" + _cnzz_eid + "&showp=" + _showp + "&ei=" + encodeURIComponent(encodeURIComponent(_content)).replace(/%257C/gi, '%7C') + "&t=&umuuid=" + _umuuid + "&h=1&rnd=" + parseInt(2147483648 * Math.random());
  }
  _moveCode(a, codeArr) {
    let d = 0;
    for (let i = 0; i < codeArr.length; i++) {
      d |= codeArr[i] << 8 * i;
    }
    return a ^ d
  }
  _code() {
    const userAgent = window.navigator.userAgent;
    const codeArr = [];
    let k = 0;
    for (let i = 0; i < userAgent.length; i++) {
      const code = userAgent.charCodeAt(i);
      codeArr.unshift(code & 255);
      if (4 <= codeArr.length) {
        k = this._moveCode(k, codeArr);
        codeArr.length = 0;
      }
    }
    if (0 < codeArr.length) {
      k = this._moveCode(k, codeArr)
    }
    return k.toString(16)
  }
}
